import os
import allure
from Interface.wechat_po.apis.wework import WeWork
from Interface.wechat_po.utils.yaml_utils import YamlUtils


@allure.feature("管理部门")
class Department(WeWork):
    """
    接口实现只描述接口信息，不做断言等
    """
    def __init__(self):
        super().__init__()
        path = os.sep.join([YamlUtils.get_frame_root_path(),"config/secrets.yaml"])
        yaml_data = YamlUtils.get_yaml_data(path)
        corpid = yaml_data.get("corpid").get("tianming")
        secret = yaml_data.get("secret").get("contact")
        # _env = os.getenv("test_env")
        self.base_url = yaml_data.get("prod").get("base_url")
        self.get_token(corpid, secret)
        # _env = os.getenv("test_env")


    # 创建部门
    def create(self, data):
        # 定义创建url
        create_url = "/department/create"
        # 定义参数
        params = {"access_token": self.access_token}
        # 发起请求, 组装调用send参数
        req = {
            "method": "POST",
            "url": create_url,
            "params": params,
            "json": data
        }
        # r = Interface.request(method="POST", url=create_url, params=params, json=data)
        r = self.send_api(req)
        # 返回请求得到的结果
        return r

    # 部门信息更新
    def update(self, data):
        # 定义创建url
        update_url = "/department/update"
        # 定义参数
        params = {"access_token": self.access_token}
        # 发起请求
        req = {
            "method": "POST",
            "url": update_url,
            "params": params,
            "json": data
        }
        # r = Interface.request(method="POST", url=update_url, json=data, params=params)
        r = self.send_api(req)
        # 返回请求得到的结果
        return r

    # 删除部门
    def delete(self, idnum):
        # 定义创建url
        delete_url = "/department/delete"
        # 定义参数
        params = {
            "access_token": self.access_token,
            "id": idnum
        }
        # 发起请求
        req = {
            "method": "GET",
            "url": delete_url,
            "params": params
        }
        # r = Interface.request(method="GET", url=delete_url, params=params)
        r = self.send_api(req)
        # 返回请求得到的结果
        return r

    # 查询部门
    def get(self, idnum=None):
        # 定义创建url
        get_url = "/department/list"
        # 定义参数
        params = {"access_token": self.access_token, "id": idnum}
        # 发起请求
        req = {
            "method": "GET",
            "url": get_url,
            "params": params
        }
        # r = Interface.request(method="GET", url=get_url, params=params)
        r = self.send_api(req)
        # 返回请求得到的结果
        return r
